package com.veo.controller;

import com.github.pagehelper.PageInfo;
import com.veo.domain.Role;
import com.veo.domain.UserInfo;
import com.veo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/userController")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/findAllUser")
    public ModelAndView findAllUser(@RequestParam(value = "page",defaultValue = "1",required = false)Integer page,@RequestParam(value = "size",defaultValue = "5",required = false)Integer size,@RequestParam(value = "searcherName",defaultValue = "",required = false)String searcherName) throws UnsupportedEncodingException {
        ModelAndView mv=new ModelAndView();
        //判断是乱码 (GBK包含全部中文字符；UTF-8则包含全世界所有国家需要用到的字符。)
        if (!(Charset.forName("GBK").newEncoder().canEncode(searcherName))) {
            //转码UTF8
            searcherName = new String(searcherName.getBytes("ISO-8859-1"), "utf-8");
        }
        List<UserInfo> allUser = userService.findAllUser(page,size,searcherName);
        //保存分页信息
        PageInfo userList=new PageInfo(allUser);
        mv.addObject("userList",userList);
        mv.addObject("searcherName",searcherName);
        mv.setViewName("user-list");
        return mv;
    }

    @RequestMapping("/addUser")
    public String addUser(UserInfo userInfo){
        String id = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        userInfo.setId(id);
        int count = userService.addUser(userInfo);
        return "redirect:findAllUser";
    }

    @RequestMapping("findUserById")
    public ModelAndView findUserById(String id){
        ModelAndView mv=new ModelAndView();
        UserInfo userById = userService.findUserById(id);
        mv.addObject("user",userById);
        mv.setViewName("user-show");
        return mv;
    }


    @RequestMapping("/findRoleToUserAdd")
    public ModelAndView findRoleToUserAdd(@RequestParam(name = "id",required = true) String id){
        ModelAndView mv=new ModelAndView();
        UserInfo userInfo = userService.findUserById(id);
        List<Role> otherRole = userService.findOtherRole(id);
        mv.addObject("user",userInfo);
        mv.addObject("roleList",otherRole);
        mv.setViewName("user-role-add");
        return mv;
    }

    @RequestMapping("/addRoleToUser")
    public String addRoleToUser(@RequestParam(name = "userId",required = true) String userId,@RequestParam(name = "ids",required = true) String[] roleIds){
        int count = userService.addRoleToUser(userId, roleIds);
        return "redirect:findAllUser";
    }

    @RequestMapping("/deleteUserByIds")
    public String deleteUserByIds(String idStr){
        if (idStr!=null && idStr.length()>0&& idStr!=""){
            String[] ids = idStr.split(",");
            for (String id : ids) {
                userService.deleteUserRoleId(id);
                userService.deleteUserById(id);
            }
        }
        return "redirect:findAllUser";
    }
}
